package com.koron.common.core.business.todoApproval.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jzp
 * @since 2022/10/10
 */
@Slf4j
public class TodoTest {

    private static final String TODO_SERVER_TEST = "http://ekp16.uat.gdhwater.com/api/sys-notify/sysNotifyTodoRestService/";
    private static final String TODO_SERVER_PROC = "https://oa.gdhwater.com:16443/api/sys-notify/sysNotifyTodoRestService/";
    private static final String TODO_USER = "eam2";
    private static final String TODO_PASSWORD = "eam731";
    private static final String DEFAULT_ACCOUNT = "test";

    // 可修改
    private static final String APP_NAME = "EAM系统";
    private static final String MODEL_NAME = "专项管理";

    private String todoServer;

    @Setter
    private String loginName;

    public TodoTest() {
        this(0, null);
    }

    public TodoTest(int env) {
        this(env, null);
    }

    /**
     * @param env 0测试环境 1正式环境
     * @param account account为空则使用defaultAccount
     */
    public TodoTest(int env, String account) {
        todoServer = env == 0 ? TODO_SERVER_TEST : TODO_SERVER_PROC;
        loginName = CharSequenceUtil.isEmpty(account) ? DEFAULT_ACCOUNT : account;
    }

    public String getTodo() {
        String api = "getTodo";
        // 定义请求参数Map
        Map<String, Object> paramBody = new HashMap<>();
        String[] split = loginName.split("[,;&]");
        List<HashMap<String, String>> targetsList = Arrays.stream(split).map(e -> {
            HashMap<String, String> map = new HashMap<>();
            map.put("LoginName", e);
            return map;
        }).collect(Collectors.toList());
        paramBody.put("targets", JSONUtil.toJsonStr(targetsList));
        paramBody.put("type", 1);
        paramBody.put("rowSize", 15);
        paramBody.put("pageNo", 1);
        HashMap<String, String> appNameMap = new HashMap<>();
        appNameMap.put("appName", APP_NAME);
        HashMap<String, String> modelNameMap = new HashMap<>();
        paramBody.put("otherCond", JSONUtil.toJsonStr(Arrays.asList(appNameMap, modelNameMap)));
        String paramStr = JSONUtil.toJsonStr(paramBody);
        String res = sendRequest(api, paramStr);
        log.info("\n获取待办\n参数：{}\n响应：{}", paramStr, res);
        return res;
    }

    public String sendTodo() {
        String api = "sendTodo";
        // 定义请求参数Map
        Map<String, Object> paramBody = new HashMap<>();
        String linkUrl = "\"https://zb.gdhwater.com:4004/\"";
        paramBody.put("appName", APP_NAME);
        paramBody.put("modelName", MODEL_NAME);
        paramBody.put("modelId", "zbsys");
        paramBody.put("subject", "周报系统流程当前节点测试");
        paramBody.put("link", linkUrl);
        paramBody.put("mobileLink", linkUrl);
        paramBody.put("padLink", linkUrl);
        paramBody.put("type", 1);
        paramBody.put("targets", "{\"LoginName\":\"" + loginName + "\"}");
        paramBody.put("docCreator", "{\"LoginName\":\"" + loginName + "\"}");
        paramBody.put("extendContent", "[{key:\"lbpmCurrNode\",titleMsgKey:\"sys-lbpmservice:lbpmSupport.STATUS_RUNNING\",value:\"领导审批\"}]");
        paramBody.put("createTime", "2022-10-10 10:10:10");
        String paramStr = JSONUtil.toJsonStr(paramBody);
        String res = sendRequest(api, paramStr);
        log.info("发送待办，参数：{}，响应：{}", paramStr, res);
        return res;
    }

    public String deleteTodo() {
        String api = "deleteTodo";
        // 定义请求参数Map
        Map<String, Object> paramBody = new HashMap<>();
        paramBody.put("appName", APP_NAME);
        paramBody.put("modelName", MODEL_NAME);
        paramBody.put("modelId", "zbsys");
        paramBody.put("optType", 1);
        paramBody.put("type", 1);
        paramBody.put("targets", "{\"LoginName\":\"" + loginName + "\"}");
        String paramStr = JSONUtil.toJsonStr(paramBody);
        String res = sendRequest(api, paramStr);
        log.info("删除待办，参数：{}，响应：{}", paramStr, res);
        return res;
    }

    public String setTodoDone(String bizId) {
        String api = "setTodoDone";
        Map<String, Object> paramBody = new HashMap<>();
        paramBody.put("appName", APP_NAME);
        paramBody.put("modelName", MODEL_NAME);
        paramBody.put("modelId", bizId);
        paramBody.put("optType", 1);
        String paramStr = JSONUtil.toJsonStr(paramBody);
        String res = sendRequest(api, paramStr);
        log.info("设待办为已办，参数：{}，响应：{}", paramStr, res);
        return res;
    }

    public String setTodoDone(String bizId, String loginName) {
        String api = "setTodoDone";
        Map<String, Object> paramBody = new HashMap<>();
        paramBody.put("appName", APP_NAME);
        paramBody.put("modelName", MODEL_NAME);
        paramBody.put("modelId", bizId);
        paramBody.put("optType", 2);
        paramBody.put("targets", "{\"LoginName\":\"" + loginName + "\"}");
        String paramStr = JSONUtil.toJsonStr(paramBody);
        String res = sendRequest(api, paramStr);
        log.info("设待办为已办，参数：{}，响应：{}", paramStr, res);
        return res;
    }

    private String sendRequest(String suffixUrl, String paramStr) {
        String url = todoServer + suffixUrl;
        log.info("请求地址：{}", url);
        HttpResponse response = HttpRequest.post(url)
                .header(Header.AUTHORIZATION, dealAuth(TODO_USER + ":" + TODO_PASSWORD))//头信息，多个头信息多次调用此方法即可
                .body(JSONUtil.toJsonStr(paramStr))//表单内容
                .timeout(20000)//超时，毫秒
                .execute();
        return response.body();
    }

    private String dealAuth(String yourEncryptedWorlds) {
        String encodedAuth = Base64.encode(yourEncryptedWorlds.getBytes(StandardCharsets.UTF_8));
        return "Basic " + encodedAuth;
    }

}
